﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using _1242026_1242133.DTO;
using _1242026_1242133.DAO;
using _1242026_1242133.BUS;
using System.Windows.Forms;

namespace _1242026_1242133
{
    public partial class Form1 : Form
    {
        
        SqlSever SQL = new SqlSever();
        string dp_MaLoaiPhong;
        //private void LoadThongTinThuePhong()
        //{
        //    DatPhongBUS BUS = new DatPhongBUS();
        //    DataTable data = BUS.LoadDSKhachHangDangThuePhong();
        //    lvThongTinThuePhong.Items.Clear();
        //    foreach (DataRow row in data.Rows)
        //    {
        //        ListViewItem lvi = new ListViewItem();//lvThongTinThuePhong.Items.Add(row["TenKhachHang"].ToString());
        //        lvi.SubItems.Add(row["TenKhachHang"].ToString());
        //        lvi.SubItems.Add(row["CMND"].ToString());
        //        lvi.SubItems.Add(row["DiaChi"].ToString());
        //        lvi.SubItems.Add(row["TenLoaiKhach"].ToString());
        //        lvi.SubItems.Add(row["TenPhong"].ToString());
        //        lvThongTinThuePhong.Items.Add(lvi);
        //    }
        //}

        private void LoadThongTinThuePhong()
        {
                DatPhongBUS BUS = new DatPhongBUS();
                DataTable data = BUS.LoadDSKhachHangDangThuePhong();
                dtThongTinKhachThuePhong.DataSource = data;
        }
        void LoadHinh()
        {
            ListViewPhong.Clear();
            PhongBUS PhongBus = new PhongBUS();
            DataTable data = PhongBus.LoadDanhSachPhong();
            foreach (DataRow row in data.Rows)
            {

                ListViewGroup lstgroup = ListViewPhong.Groups[row["MaLoaiPhong"].ToString()];

                if (lstgroup == null)
                {
                    lstgroup = new ListViewGroup(row["MaLoaiPhong"].ToString(), row["MaLoaiPhong"].ToString());
                    ListViewPhong.Groups.Add(lstgroup);
                }
                ListViewItem item = new ListViewItem(lstgroup);
                item.Text = row["MaPhong"].ToString();
                item.ImageIndex = int.Parse(row["TinhTrangPhong"].ToString());
                ListViewPhong.Items.Add(item);
                ListViewPhong.View = View.LargeIcon;

            }
            ListViewPhong.Refresh();

        }
        public Form1()
        {
            InitializeComponent();
            LoadThongTinThuePhong();
            LoadHinh();
        }
        // method of form
        // Phong


        private void LoadDanhSachPhong()
        {
            PhongBUS BUS = new PhongBUS();
            DataTable data = BUS.LoadDanhSachPhong();
            dgPhong.DataSource = data;
        }

        private void LoadDanhSachLoaiPhong(ComboBox cb)
        {
            LoaiPhongBUS BUS = new LoaiPhongBUS();
            DataTable data = BUS.LoadDanhSachLoaiPhong();
            cb.DataSource = data;
            cb.DisplayMember = "TenLoaiPhong";
            cb.ValueMember = "MaLoaiPhong";
        }

        private bool KiemTraNhapPhong()
        {
            if((txtMaPhong.Text == "") || (txtTenPhong.Text=="" )|| (cbLoaiPhong.Text==""))
            {
                MessageBox.Show("Chưa Nhập Đủ Thông Tin","Thông Báo",MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        // Khach Hang
        private bool KiemTraNhapKhachHang()
        { 
            if(txtMaKhachHang.Text==""||txtTenKhachHang.Text== "" || txtCMND.Text==""||txtDiaChi.Text=="")
            {
                MessageBox.Show("Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private bool KiemTraDatPhong()
        {
            if (txtDPHoTen.Text== "" || txtCMND.Text == "" || txtDiaChi.Text == "")
            {
                MessageBox.Show("Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        private void LoaiDanhSachKhachHang()
        {
            KhachHangBUS BUS = new KhachHangBUS();
            dgKhachHang.DataSource = BUS.LoadDanhSachKhachHang();
        }
        private void LoadDanhSachLoaiKhachHang(ComboBox cb)
        {
            LoaiKhachBUS BUS = new LoaiKhachBUS();
            cb.DataSource = BUS.LoadDanhSachLoaiKhach();
            cb.DisplayMember = "TenLoaiKhach";
            cb.ValueMember = "MaLoaiKhach";
        }

        private void LoadPhieuThuePhong()
        {
            DatPhongBUS BUS = new DatPhongBUS();
            dgPhieuThuePhong.DataSource = BUS.LoadDanhSachPhieuThue();
        }
        // end
        // Dat Phong
        private void LoadDatPhong()
        {
            LoadDanhSachLoaiPhong(cbDPMaLoaiPhong);
            LoadDanhSachLoaiKhachHang(cbDPLoaiKhach);
            PhongBUS BUS = new PhongBUS();
        }

        // Phong
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDanhSachPhong();
            LoaiDanhSachKhachHang();
            LoadDanhSachLoaiKhachHang(cbLoaiKhachHang);
            LoadDatPhong();
            LoadDanhSachLoaiPhong(cbLoaiPhong);
            LoadPhieuThuePhong();
        }


        private void cbLoaiPhong_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            LoaiPhongBUS LoaiPhongBus = new LoaiPhongBUS();
            string MaLoaiPhong = cbLoaiPhong.SelectedValue.ToString();
            DataTable Gia = LoaiPhongBus.LoadGiaTheoLoaiPhong(MaLoaiPhong);
            if (Gia.Rows.Count > 0)
            {
                DataRow dr = Gia.Rows[0];
                txtDonGia.Text = dr["GiaLoaiPhong"].ToString();
            }
        }
        private void cbDPMaLoaiPhong_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            DatPhongBUS DatPhongBus = new DatPhongBUS();
            LoaiPhongBUS LoaiPhongBus = new LoaiPhongBUS();
            string MaLoaiPhong = cbDPMaLoaiPhong.SelectedValue.ToString();
            dp_MaLoaiPhong = cbDPMaLoaiPhong.SelectedValue.ToString();
            DataTable Gia = LoaiPhongBus.LoadGiaTheoLoaiPhong(MaLoaiPhong);
            if (Gia.Rows.Count > 0)
            {
                DataRow dr = Gia.Rows[0];
                txtDPGiaTienPhong.Text = dr["GiaLoaiPhong"].ToString();
            }

            DataTable data = DatPhongBus.LoadDSPhongTheoLoaiPhong(MaLoaiPhong);
            cbDPTenPhong.DataSource = data;
            cbDPTenPhong.DisplayMember = "TenPhong";
            cbDPTenPhong.ValueMember = "MaPhong";

        }
        private void butThemPhong_Click(object sender, EventArgs e)
        {
            PhongBUS PhongBus = new PhongBUS();
            if (KiemTraNhapPhong())
            {
                PhongDTO Phong = new PhongDTO(txtMaPhong.Text, txtTenPhong.Text, txtGhiChu.Text,cbLoaiPhong.SelectedValue.ToString());
                DataTable data = PhongBus.ThemPhong(Phong);
                DataRow dr = data.Rows[0];
                MessageBox.Show(dr["ErrMsg"].ToString(),"Thông Báo",MessageBoxButtons.OK);
                LoadDanhSachPhong();
                LoadHinh();
            }
        }
        private void dgPhong_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            txtMaPhong.Text = dgPhong.CurrentRow.Cells["colMaPhong"].Value.ToString();
            txtTenPhong.Text = dgPhong.CurrentRow.Cells["colTenPhong"].Value.ToString();
            cbLoaiPhong.Text = dgPhong.CurrentRow.Cells["colTenLoaiPhong"].Value.ToString();
            txtGhiChu.Text = dgPhong.CurrentRow.Cells["colGhiChu"].Value.ToString();
        }
        private void butXoaPhong_Click(object sender, EventArgs e)
        {
           PhongBUS PhongBus = new PhongBUS();
           var a = MessageBox.Show("Bạn Có Chắc Muốn Xóa Phòng Này", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
           if (a == DialogResult.Yes)
           {
               PhongDTO PhongDto = new PhongDTO();
               PhongDto.MaPhong = txtMaPhong.Text;
               DataTable data =  PhongBus.XoaPhong(PhongDto);
               DataRow dr = data.Rows[0];
               MessageBox.Show(dr["ErrMsg"].ToString());
               LoadDanhSachPhong();              
           }
        }
        private void ButtonX4_Click(object sender, EventArgs e)
        {
            PhongBUS PhongBus = new PhongBUS();
            var a = MessageBox.Show("Bạn Có Chắc Muốn Xóa Phòng Này", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (a == DialogResult.Yes)
            {
                PhongDTO Phong = new PhongDTO(txtMaPhong.Text, txtTenPhong.Text, cbLoaiPhong.SelectedValue.ToString(), txtGhiChu.Text);
                DataTable data = PhongBus.SuaPhong(Phong);
                DataRow dr = data.Rows[0];
                MessageBox.Show(dr["ErrMsg"].ToString(), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LoadDanhSachPhong(); 
            }
        }
        private void butQuanLyLoaiPhong_Click(object sender, EventArgs e)
        {
            frmQuanLyLoaiPhong frm = new frmQuanLyLoaiPhong();
            frm.Show();
        }
        //
        private void Button1_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.Show();
        }
        private void buttonX5_Click(object sender, EventArgs e)
        {
            frmQuanLyLoaiKhach frm = new frmQuanLyLoaiKhach();
            frm.Show();
        }
        // Khach Hang
        private void butThemKhachHang_Click(object sender, EventArgs e)
        {
            if (KiemTraNhapKhachHang())
            {
                KhachHangBUS BUS = new KhachHangBUS();
                KhachHangDTO KhachHangDto = new KhachHangDTO(txtMaKhachHang.Text, txtTenKhachHang.Text, txtCMND.Text, txtDiaChi.Text, cbLoaiKhachHang.SelectedValue.ToString());
                DataTable data = BUS.ThemKhachHang(KhachHangDto);
                DataRow dr = data.Rows[0];
                MessageBox.Show(dr["ErrMsg"].ToString(),"Thông Báo");
                LoaiDanhSachKhachHang();
            }
        }
        int SelectedItemIndex(ListView listview)
        {
            for (int i = 0; i < listview.Items.Count; i++)
            {
                if (listview.Items[i].Selected) { return i; }
            }
            return 0;
        }
        private void butDatPhong_Click(object sender, EventArgs e)
        {
            //if (KiemTraDatPhong())
            //{
                DatPhongDAO DAO = new DatPhongDAO();
                string ngay = dtNgayDatPhong.Value.ToString();
                DatPhongDTO DatPhongDto = new DatPhongDTO(cbDPTenPhong.SelectedValue.ToString(), ngay, txtDPHoTen.Text, txtDPCMND.Text, txtDPDiaChi.Text, cbDPLoaiKhach.SelectedValue.ToString());
                DataTable data = DAO.ThemPhieuThuePhong(DatPhongDto);
                DataRow dr = data.Rows[0];
                MessageBox.Show(dr["ErrMsg"].ToString(), "Thông Báo");
                LoaiDanhSachKhachHang();
                LoadDanhSachPhong();
                LoadHinh();
                LoadThongTinThuePhong();
                cbDPTenPhong.SelectedValue.ToString();
        //}
        }
        private void ListViewPhong_DoubleClick(object sender, EventArgs e)
        {
            int vitri = ListViewPhong.SelectedIndices[0];
            ListViewItem item = ListViewPhong.SelectedItems[0];
           // cbLoaiKhachHang.DisplayMember = item.SubItems[0].Text;
            cbDPTenPhong.SelectedValue = "Phong 01A";
            MessageBox.Show(item.SubItems[0].Text);

        }
        private void ListViewPhong_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            //MessageBox.Show(ListViewPhong.SelectedIndices[0].ToString());
            // if (ListViewPhong.SelectedItems[i].ToString()!="")
            {
                //ListViewItem item = ListViewPhong.SelectedItems[0];
                //ac = item.SubItems[0].Text;
                //cbDPLoaiKhach.SelectedIndex = ac;
                //int vitri = ListViewPhong.SelectedIndices[0];
                //if (ListViewPhong.Items[vitri].ImageIndex == 0)
                //    ListViewPhong.Items[vitri].ImageIndex = 1;
                //return;

            }
        }
        private void txtDPCMND_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
                e.Handled = true;
        }

              
    }
}
